package com.budwk.app.miniapp.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.common.result.ResultCode;
import com.budwk.starter.common.page.PageUtil;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import com.budwk.app.miniapp.models.WxEventAttest;
import com.budwk.app.miniapp.services.WxeventattestService;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.*;
import org.nutz.lang.Strings;
import com.budwk.starter.common.openapi.annotation.*;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletRequest;

@IocBean
@At("/platform/miniapp/event/attest")
@ApiDefinition(tag = "微信小程序-事件上报")
@Slf4j
public class WxeventattestController {
    @Inject
    private WxeventattestService wxEventAttestService;

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "分页查询事件上报")
    @ApiFormParams(
            value = {
                    @ApiFormParam(name = "pageNo", example = "1", description = "页码", type = "integer"),
                    @ApiFormParam(name = "pageSize", example = "10", description = "页大小", type = "integer"),
                    @ApiFormParam(name = "pageOrderName", example = "createdAt", description = "排序字段"),
                    @ApiFormParam(name = "pageOrderBy", example = "descending", description = "排序方式")
            },
            implementation = WxEventAttest.class
    )
    @ApiResponses(
            implementation = WxEventAttest.class
    )
    @SaCheckLogin
    public Result<?> list(@Param("..") WxEventAttest bean,
                          @Param("pageNo") int pageNo,
                          @Param("pageSize") int pageSize,
                          @Param("pageOrderName") String pageOrderName,
                          @Param("pageOrderBy") String pageOrderBy) {
        Cnd cnd = Cnd.NEW();
        if (Strings.isNotBlank(pageOrderName) && Strings.isNotBlank(pageOrderBy)) {
            cnd.orderBy(pageOrderName, PageUtil.getOrder(pageOrderBy));
        }
        if (bean.getId() != null && Strings.isNotBlank(bean.getId().toString())) {
            cnd.and("id", "=", bean.getId());
        }
        if (bean.getEventId() != null && Strings.isNotBlank(bean.getEventId().toString())) {
            cnd.and("event_id", "=", bean.getEventId());
        }
        if (bean.getOpenid() != null && Strings.isNotBlank(bean.getOpenid().toString())) {
            cnd.and("open_id", "=", bean.getOpenid());
        }
        if (bean.getUserName() != null && Strings.isNotBlank(bean.getUserName().toString())) {
            cnd.and("user_name", "=", bean.getUserName());
        }
        if (bean.getLatitude() != null && Strings.isNotBlank(bean.getLatitude().toString())) {
            cnd.and("latitude", "=", bean.getLatitude());
        }
        if (bean.getLongitude() != null && Strings.isNotBlank(bean.getLongitude().toString())) {
            cnd.and("longitude", "=", bean.getLongitude());
        }
        if (bean.getAddress() != null && Strings.isNotBlank(bean.getAddress().toString())) {
            cnd.and("address", "=", bean.getAddress());
        }
        if (bean.getReportType() != null && Strings.isNotBlank(bean.getReportType().toString())) {
            cnd.and("report_type", "=", bean.getReportType());
        }
        if (bean.getCreateTime() != null && Strings.isNotBlank(bean.getCreateTime().toString())) {
            cnd.and("create_time", "=", bean.getCreateTime());
        }
        if (bean.getPictureUrl() != null && Strings.isNotBlank(bean.getPictureUrl().toString())) {
            cnd.and("picture_url", "=", bean.getPictureUrl());
        }
        if (bean.getReportContent() != null && Strings.isNotBlank(bean.getReportContent().toString())) {
            cnd.and("report_content", "=", bean.getReportContent());
        }
        if (bean.getRemark() != null && Strings.isNotBlank(bean.getRemark().toString())) {
            cnd.and("remark", "=", bean.getRemark());
        }
        if (bean.getEventDistance() != null && Strings.isNotBlank(bean.getEventDistance().toString())) {
            cnd.and("event_distance", "=", bean.getEventDistance());
        }

        return Result.data(wxEventAttestService.listPage(pageNo, pageSize, cnd));
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "新增事件上报")
    @ApiFormParams(
            implementation = WxEventAttest.class
    )
    @ApiResponses
    @SLog(tag = "新增事件上报", value = "${args[0].${table.pkName}}")
    @SaCheckLogin
    public Result<?> create(@Param("..") WxEventAttest wxEventAttest, HttpServletRequest req) {
        wxEventAttest.setOpenid(SecurityUtil.getUserId());
        wxEventAttestService.insert(wxEventAttest);
        return Result.success();
    }

    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "修改事件上报")
    @ApiFormParams(
            implementation = WxEventAttest.class
    )
    @ApiResponses
    @SLog(tag = "修改事件上报", value = "${args[0].${table.pkName}}")
    @SaCheckLogin
    public Result<?> update(@Param("..") WxEventAttest wxEventAttest, HttpServletRequest req) {
        wxEventAttest.setUpdatedBy(SecurityUtil.getUserId());
        wxEventAttestService.updateIgnoreNull(wxEventAttest);
        return Result.success();
    }

    @At("/get")
    @Ok("json")
    @POST
    @ApiOperation(name = "获取事件上报详情")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    @SaCheckLogin
    public Result<?> getData(@Param("id") String id, HttpServletRequest req) {
        WxEventAttest wxEventAttest = wxEventAttestService.fetch(id);
        if (wxEventAttest == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        return Result.data(wxEventAttest);
    }

    @At("/delete")
    @Ok("json")
    @POST
    @ApiOperation(name = "删除事件上报")
    @ApiFormParams(
            {
                    @ApiFormParam(name = "id", description = "唯一标识", required = true, check = true)
            }
    )
    @SLog(tag = "删除事件上报", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> delete(@Param("id") String id, HttpServletRequest req) {
        WxEventAttest wxEventAttest = wxEventAttestService.fetch(id);
        if (wxEventAttest == null) {
            return Result.error(ResultCode.NULL_DATA_ERROR);
        }
        wxEventAttestService.delete(id);
        req.setAttribute("_slog_msg", wxEventAttest.getId());
        return Result.success();
    }
}
